/**
 * Sonar iOS Client Application
 * Extension for Sonar, open source software quality management tool.
 * Copyright (C) 2011 Maris Plots
 * mailto: maris.plots@gmail.com
 *
 * Sonar iOS Client Application is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * Sonar iOS Client Application is distributed in the hope that it will be 
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Sonar iOS Client Application; if not, write to the Free 
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
 */

#import "AbstractRESTServiceHandlerDelegate.h"
#import "ResourceListViewController.h"
#import "UINib+SonarAdditions.h"
#import "GetResourcesService.h"
#import "ResourceListCell.h"
#import "UIKit+Utilities.h"
#import "Resource.h"

@interface ResourceListViewController() <AbstractRESTServiceHandlerDelegate>
@property (nonatomic, retain) UINib *cellNib;
@property (nonatomic, retain) NSArray *resourceList;
- (void)sonar_internalRelease;
@end

@implementation ResourceListViewController
@synthesize cellNib = cellNib_;
@synthesize refreshButton = refreshButton_;
@synthesize resourceList = resourceList_;

#pragma mark -
#pragma mark Memory Management
#pragma mark -
- (void)dealloc{
    [self sonar_internalRelease];
    [resourceList_ release]; resourceList_ = nil;
    [super dealloc];
}

- (void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
    self.cellNib = nil;
}

- (void)viewDidUnload{
    [super viewDidUnload];
    [self sonar_internalRelease];
}

- (void)sonar_internalRelease{
    self.cellNib = nil;
    self.refreshButton = nil;
}

#pragma mark -
#pragma mark Interface methods
#pragma mark -
- (void)awakeFromNib{
    [super awakeFromNib];
    [self refreshButtonTapped:refreshButton_];
}


#pragma mark -
#pragma mark View lifecycle methods
#pragma mark -
- (void)viewDidLoad{
    [super viewDidLoad];				

    self.navigationController.navigationBar.tintColor = RGB(75, 159, 213);
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orient{
    return YES;
}

#pragma mark -
#pragma mark Action methods
#pragma mark -
- (IBAction)refreshButtonTapped:(UIBarButtonItem *)button{
    [GetResourcesService executeServiceWithDelegate:self];
}

#pragma mark -
#pragma mark UITableViewDataSource methods
#pragma mark -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView 
    numberOfRowsInSection:(NSInteger)section{
    
    return [resourceList_ count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView 
    cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *id_ = kResourceListCellId;
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:id_];
    if (cell == nil) {
        cell = [self.cellNib sonar_instantiate];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
    }
    return cell;
}


#pragma mark -
#pragma mark UITableViewDelegate methods
#pragma mark -
- (void)tableView:(UITableView *)tableView 
    didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}

- (void)tableView:(UITableView *)tableView 
    willDisplayCell:(UITableViewCell *)cell 
    forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    Resource *resource = [resourceList_ objectAtIndex:indexPath.row];
    cell.textLabel.text = resource.objectName;

    if (indexPath.row%2==0) {
        cell.backgroundColor = RGB(244,244,244);
    }else{
        cell.backgroundColor = RGB(255,255,255);
    }
    
}

#pragma mark -
#pragma Hidden property acessors
#pragma mark -
- (UINib *)cellNib{
    @synchronized(self){
        if (!cellNib_) {
            cellNib_ = [UINib nibWithNibName:@"ResourceListCell" bundle:nil];
            [cellNib_ retain];
        }
        return cellNib_;
    }
}

#pragma mark -
#pragma mark Hidden methods
#pragma mark -

#pragma mark -
#pragma mark AbstractRESTServiceHandlerDelegate methods
#pragma mark -
- (void)serviceHandler:(AbstractRESTServiceHandler *)handler
    didReceiveResponse:(NSMutableArray *)response{
    self.resourceList = response;
    [self.tableView reloadData];
}

- (void)serviceHandle:(AbstractRESTServiceHandler *)handler
     didFailWithError:(NSError *)error 
             inModule:(NSString *)module{
    
}

@end
